package gen1us.jutw.CreateConfig;

/*
 Copyright 2009 Evgeny Yusov

 This file is part of JUTW.

 JUTW is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 JUTW is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with JUTW.  If not, see http://www.gnu.org/licenses/.
 */
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import gen1us.jutw.utils.CheckConfig;

class JutwFrame {

	public JutwFrame(Config config) {
		this.config = config;
		frame = new JFrame();
		mainPanel = new JPanel(new SpringLayout());
	}

	public void make() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			System.err.println("Unable to change lookAndFeel");
			e.printStackTrace();
		}
		frame.setTitle("Jutw beta");
		JPanel buttonPanel = new JPanel();
		frame.add(buttonPanel, BorderLayout.SOUTH);
		final JButton advButton = new JButton("Advanced");
		buttonPanel.add(advButton);
		JButton okButton = new JButton("OK");
		buttonPanel.add(okButton);

		frame.add(mainPanel);
		int cut = config.getCut();
		addFields(0, cut - 1);

		SpringUtilities.makeCompactGrid(mainPanel, cut, 2, 6, 6, 6, 6);

		okButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {
				saveToValues();
				config.saveToFile();
				frame.setVisible(false);
				CheckConfig.start();
			}
		});
		
		advButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Boolean show = isAdvShow();
				if (!show) {
					frame.pack();
					advButton.setText("Hide advanced");
					setAdvShow(true);
				} else{
					hideAdv();
					advButton.setText("Advanced");
					setAdvShow(false);
				}
			}
		});

		frame.setResizable(false);
		frame.pack();
		frameDefaultWidth = frame.getWidth();
		frameDefaultHeight = frame.getHeight();
		toCenter();
		frame.setVisible(true);
		addFields(cut, config.size() - 1);
		SpringUtilities
				.makeCompactGrid(mainPanel, config.size(), 2, 6, 6, 6, 6);
	}

	private void addFields(int first, int last) {
		for (int tid = first; tid <= last; tid++) {
			if (config.isProperty(tid)) {
				Label label = new Label(config.getDesc(tid));
				mainPanel.add(label);
				JTextField field = new JTextField(config.getValue(tid), 15);
				mainPanel.add(field);
				textFields.add(field);
				types.add(new Type(1, textFields.size() - 1));
			} else if (config.isPropertyPass(tid)) {
				Label label = new Label(config.getDesc(tid));
				mainPanel.add(label);
				JPasswordField field = new JPasswordField("", 15);
				mainPanel.add(field);
				passFields.add(field);
				types.add(new Type(2, passFields.size() - 1));
			} else if (config.isComboBox(tid)) {
				Label label = new Label(config.getDesc(tid));
				mainPanel.add(label);
				final JComboBox cBox = new JComboBox();
				String[] items = config.getComboBoxItems(tid);
				for (String s : items) {
					cBox.addItem(s);
				}
				cBox.addActionListener(new ComboBoxAction(tid));

				// cBox.addItem("test");
				// config.selectMenuItem(tid, "GTalk");
				mainPanel.add(cBox);
				types.add(new Type(3, textFields.size() - 1));
			} else if (config.isCheckBox(tid)) {
				Label label = new Label(config.getDesc(tid));
				mainPanel.add(label);
				JCheckBox checkBox = new JCheckBox();
				mainPanel.add(checkBox);
				checkBoxes.add(checkBox);
				types.add(new Type(4, checkBoxes.size() - 1));
			}

		}
	}
	
	void hideAdv(){
		frame.setSize(frameDefaultWidth, frameDefaultHeight);
	}

	void toCenter() {
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		int y = ((int) dim.getHeight() - frame.getHeight()) / 2;
		int x = ((int) dim.getWidth() - frame.getWidth()) / 2;
		frame.setLocation(x, y);
	}

	private void saveToValues() {
		for (int tid = 0; tid < types.size(); tid++) {
			int type = types.get(tid).getType();
			int index = types.get(tid).getIndex();
			if (type == 1) { // is textField
				String s = textFields.get(index).getText();
				config.setParamValue(tid, s);
			} else if (type == 2) { // is passField
				char[] c = passFields.get(index).getPassword();
				String s = new String(c);
				config.setParamValue(tid, s);
			} else if (type == 4) { // is checkBox
				Boolean v = checkBoxes.get(index).isSelected();
				config.setCheckBoxValue(tid, v);
			}
		}
	}

	private int getType(int tid) {
		return types.get(tid).getType();
	}

	class ComboBoxAction implements ActionListener {

		public ComboBoxAction(int tid) {
			this.tid = tid;
		}

		public void actionPerformed(ActionEvent event) {
			JComboBox cBox = (JComboBox) event.getSource();
			String label = cBox.getSelectedItem().toString();
			config.enableLI(tid);
			int[] getTidsChangedParamsLI = config.getChangedParamsLIId(tid);
			for (int tempTid : getTidsChangedParamsLI) {
				String value = config.getValue(tempTid);
				// int index = types.get(tempTid).getIndex();
				// textFields.get(index).setText(value);
				changeField(tempTid, value);
			}
			config.selectMenuItem(tid, label);
			int[] changedParams = config.getChangedParamsId(tid, label);
			for (int tempTid : changedParams) {
				String value = config.getValue(tempTid);
				// int index = types.get(tempTid).getIndex();
				// textFields.get(index).setText(value);
				changeField(tempTid, value);
			}
		}

		final int tid;
	}
	
	Boolean isAdvShow(){
		return advShow;
	}
	void setAdvShow(Boolean b){
		advShow = b;
	}

	private final ArrayList<Type> types = new ArrayList<Type>();
	private final ArrayList<JTextField> textFields = new ArrayList<JTextField>();
	private final ArrayList<JPasswordField> passFields = new ArrayList<JPasswordField>();
	private final ArrayList<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();
	private final JPanel mainPanel;
	private final JFrame frame;
	private final Config config;
	private Boolean advShow = false;
	private int frameDefaultWidth;
	private int frameDefaultHeight;
	
	private class Type {

		public Type(int type, int index) {
			this.type = type;
			this.index = index;
		}

		public int getType() {
			return type;
		}

		public int getIndex() {
			return index;
		}

		private final int index;
		private final int type;
	}

	private void changeField(int tid, String value) {
		int type = getType(tid);
		int index = types.get(tid).getIndex();
		if (type == 1) {
			textFields.get(index).setText(value);
		} else if (type == 4) {
			if (value.equals("yes")) {
				checkBoxes.get(index).setSelected(true);
			} else {
				checkBoxes.get(index).setSelected(false);
			}
		}

	}
}
